package com.leruan.aasimp.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.dao.MaintainDao;
import com.leruan.aasimp.entity.CarMaintenanceRecord;

public class MaintainDaoImpl extends HibernateDaoSupport implements MaintainDao{

	public void addMaintain(CarMaintenanceRecord maintenanceRecord) {
		Session session = this.getSession();
		session.save(maintenanceRecord);
	}

	public CarMaintenanceRecord getMaintainByMID(Long maintainId) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(CarMaintenanceRecord.class);
		Criterion  c1 = Expression.eq("maintainId", maintainId);
		cri.add(c1);
		CarMaintenanceRecord result = (CarMaintenanceRecord) cri.uniqueResult();
		return result;
	}

	public List<CarMaintenanceRecord> getMaintains() {
		// TODO Auto-generated method stub
		return null;
	}

	public void updateMaintain(CarMaintenanceRecord maintenanceRecord) {
		Session session = this.getSession();
		session.update(maintenanceRecord);
	}

	@SuppressWarnings("unchecked")
	public List<CarMaintenanceRecord> getMaintainByUm(String handperson) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(CarMaintenanceRecord.class);
		Criterion  c1 = Expression.eq("handperson", handperson);
		Criterion  c2 = Expression.eq("status", "1");
		cri.add(c1);
		cri.add(c2);
		List<CarMaintenanceRecord> list = cri.list();
		return list;
	}

	public List<CarMaintenanceRecord> getMaintainByVin(String vin) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(CarMaintenanceRecord.class);
		Criterion  c1 = Expression.eq("vin", vin);
		Criterion  c2 = Expression.eq("status", "1");
		cri.add(c1);
		cri.add(c2);
		List<CarMaintenanceRecord> list = cri.list();
		return list;
	}

}
